package cn.kuang.controller;

import cn.kuang.pojo.Administrator;
import cn.kuang.service.AdminService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * @author 徽州大都督
 * @date 2020/7/19
 */
@RestController
@RequestMapping("adminAndRole")
public class AdminController {

    @Autowired
    private AdminService adminService;

    /**
     * 查询所有
     *
     * @return
     */
    @GetMapping("findAll")
    public List<Administrator> findAll() {

        //System.out.println ("admin访问了...");
        return adminService.findAll ();
    }

    /**
     * 添加
     *
     * @param administrator
     * @return
     */
    @PostMapping("addAdmin")
    public String addAdmin(@RequestBody Administrator administrator) {

        System.out.println (administrator);
        //System.out.println ("访问了");

        System.out.println (administrator.getAdminName ());
        if (administrator.getStatus () == null || StringUtils.isEmpty (administrator.getAdminName ()) || StringUtils.isEmpty (administrator.getPassword ())
                || StringUtils.isEmpty (administrator.getAdminEmail ()) || StringUtils.isEmpty (administrator.getAdminTelephone ())
        ) {
            return "failure";
        } else {
            int row = adminService.addAdmin (administrator);
            if (row > 0) {
                return "success";
            } else {
                return "failure";
            }
        }

        //System.out.println (administrator);

    }

    /**
     * 验证登录
     *
     * @param username
     * @param password
     * @return
     */
    @PostMapping("login/{username}/{password}")
    public Administrator login(@PathVariable("username") String username, @PathVariable("password") String password) {

        if (username == null || password == null) {
            return null;
        }

        Administrator administrator = adminService.login (username, password);

        //System.out.println (administrator);
        //administrator.forEach (System.out::println);

        if (administrator != null) {
            return administrator;
        } else {
            //返回一个空的对象
            Administrator admin = new Administrator ();
            return admin;
        }


    }


    /**
     * 根据id查询
     *
     * @param aid
     * @return
     */
    @GetMapping("findAdminById/{id}")
    public Administrator findAdminById(@PathVariable("id") String aid) {
        //System.out.println (aid);
        Administrator administrator = adminService.findAdminById (aid);
        //System.out.println (administrator);
        return administrator;
    }

    /**
     * 编辑信息回显
     *
     * @param adminId
     * @param administrator
     * @return
     */
    @PutMapping("updateAdmin/{adminId}")
    public String updateAdmin(@PathVariable("adminId") String adminId, @RequestBody Administrator administrator) {
        //System.out.println ("访问了");
        //System.out.println (administrator);
        //System.out.println (adminId);
        int rows = adminService.updateById (adminId, administrator);

        if (rows > 0) {
            return "success";
        }
        return "error";
    }

    /**
     * 删除
     *
     * @param id
     * @return
     */
    @DeleteMapping("delAdmin/{id}")
    public String delAdmin(@PathVariable("id") String id) {
        //System.out.println ("访问了");
        //System.out.println (id);
        int row = adminService.delAdmin (id);
        if (row > 0) {
            return "success";
        }
        return "error";
    }

    /**
     * 模糊搜索
     */
    @GetMapping("findByKey/{newKey}")
    public List<Administrator> findByKey(@PathVariable("newKey")String newKey){
        if (newKey==null){
            return this.findAll ();
        }

        //System.out.println ("访问了");
        return adminService.findByKey(newKey);
    }


    /**
     * 分页
     * @return
     */
    @GetMapping("findPage/{page}/{size}")
    public List<Administrator> findPage(@PathVariable("page")Integer page,@PathVariable("size")Integer size){
        System.out.println (page);
        System.out.println (size);

        return null;
    }
}
